import { ref } from "vue";

import { defineStore } from "pinia";

const UseuserPermission = defineStore(
  "UserPermission",
  () => {
    const UserPermission = localStorage.getItem("UserPermission");
    const routerList = ref([]);
    UserPermission
      ? (routerList.value = JSON.parse(UserPermission))
      : (routerList.value = []);
    const dynamicMenu = (value) => {
      console.log(value);
      const modules = import.meta.glob("../views/**/**/*.vue");
      console.log(modules);
      function routerSet(router) {
        router.forEach((route) => {
          if (!route.children) {
            const url = `../views${route.meta.path}/index.vue`;
            console.log(url);
            console.log(modules[url]);

            route.component = modules[url];
          } else {
            routerSet(route.children);
          }
        });
        // 判读没有子菜单，拼接路由数据
      }
      routerSet(value);
      routerList.value = value;
      console.log(routerList.value);
    };
    return {
      routerList,
      dynamicMenu,
    };
  },
  {
    persist: true,
  }
);
export default UseuserPermission;
